﻿//模块基类
Ext.namespace('app');

app.module = function(tab){
    this.main = tab;
    this.init();
}

Ext.extend(app.module, Ext.util.Observable, {
    init : Ext.emptyFn
});

//主程序类
app.exam = function(){
    this.init();
}

Ext.extend(app.exam, Ext.util.Observable, {
    currentFormValues:{},
    init: function(){
        var main = this;
        //如果未修改过信息，打开修改
        Ext.Ajax.request({
            method:'POST',
            url: 'student/getStudentState.do',
            params: "",
            scope: this,
            async: false,   //改为同步请求，给全局变量赋值
            success: function(response){
                var json = Ext.util.JSON.decode(response.responseText);
                if(json.state==0){
                    Ext.apply(this.currentFormValues, {
                        id: json.student.id,
                        cardid: json.student.cardid,
                        number: json.student.number,
                        password: json.student.password,
                        username: json.student.username,
                        phone: json.student.phone,
                        email: json.student.email
                    });
                    if(!main.form){
                        main.form = main.getForm();
                    }
                    if(!main.dlg){
                        main.dlg = main.getDialog();
                    }
                    main.dlg.setTitle('请修改个人信息');
                    main.dlg.show();
                }
            },
            failure: function(form, action) {
                var info = action.result.info;
                Ext.Msg.alert('提示', info);
            }
        });
        this.tree = new Ext.tree.TreePanel({
            id: 'treeMenu',
            xtype: 'treepanel',
            width: 190,
            collapsible: true,
            title: '在线考试',
            region: 'west',
            split: true,
            lines:false,        // 去掉树的线
            rootVisible : false, // 根节点是否可见
            root: {
                id: 0,
                text: '根节点',
                expanded: true,
                text: 'Tree Node',
                children: [
                    {
                        id: 1,
                        text: '参加考试',
                        qtip: '点击查看所有能够考试的科目',
                        leaf: true
                    },
                    {
                        id: 2,
                        text: '我的成绩',
                        qtip: '点击查看成绩',
                        leaf: true
                    },
                    {
                        text: '我的账户',
                        expanded: true,
                        children: [
                            {
                                id: 3,
                                text: '个人资料',
                                qtip: '点击修改个人资料',
                                leaf: true
                            },
                            {
                                id: 4,
                                text: '修改密码',
                                qtip: '点击修改密码',
                                leaf: true
                            }
                        ]
                    }
                ]
            }
        });
        this.tree.on('click',this.clickTree, this);

        var viewport = new Ext.Viewport({
            layout: 'border',
            items: [
                {
                    id: 'maintabs',
                    xtype: 'tabpanel',
                    title: '在线考试系统',
                    margins:'0 5 0 0',
                    activeTab: 0,
                    autoScroll: true,
                    region: 'center',
                    items: [
                        {
                            xtype: 'panel',
                            title: '首页'
                        }
                    ]
                },
                this.tree,
                {
                    xtype: 'panel',
                    collapsible: true,
                    frame: true,
                    title: '在线考试系统',
                    region: 'north',
                    contentEl: 'north'
                },
                {
                    xtype: 'box',
                    height: 17,
                    region: 'south',
                    contentEl: 'south'
                }
            ]
        });
        this.body = Ext.getCmp('maintabs');
        this.body.on('tabchange', this.changeTab, this);
        this.loadMask = new Ext.LoadMask(this.body.body);
    },
    //切换TabPanel标签方法
    changeTab: function(p,t){
        //如果存在相应树节点，就选中,否则就清空选择状态
        var id = t.id.replace('tab-','');
        var node = this.tree.getNodeById(id);
        if(node){
            this.tree.getSelectionModel().select(node);
        }else{
            this.tree.getSelectionModel().clearSelections();
        }
    },

    //点击菜单方法(node:被点击的节点)
    clickTree: function(node){
        if(node.id==1 || node.id==2){
            var id = 'tab-' + node.id;
            var tab = Ext.getCmp(id);
            if(!tab){
                tab = this.body.add(new Ext.Panel({
                    id: id,
                    title: node.text,
                    layout: 'fit',
                    closable: true
                }));
                this.body.setActiveTab(tab);
                //加载模块
                this.loadModel(node.id,tab);
            }else{
                this.body.setActiveTab(tab);
            }
        } else if(node.id==3){

        } else if(node.id==4){

        }
    },

    //加载模块方法(id:模块ID;tab:模块显示在哪里)
    loadModel: function(id,tab){
        //定义模块变量
        var model;
        if(this[id]){
            //如果模块类已存在，就直接实例化
            model = new this[id](tab);
        }else{
            this.loadMask.show();
            Ext.Ajax.request({
                method:'GET',
                url: 'module/stu-'+id+'.js',
                scope: this,
                success: function(response){
                    //获取模块类
                    this[id] = eval(response.responseText);
                    //实例化模块类
                    model = new this[id](tab);
                    this.loadMask.hide();
                }
            });
        }
    },

    getForm: function() {
        var main = this;
        var form = new Ext.form.FormPanel({
            defaultType: 'textfield',
            bodyStyle: 'padding:5px',
            buttonAlign: 'center',
            url:'student/save.do',
            labelWidth: 80,
            defaults: {
                anchor: '90%'
            },
            items: [
                {
                    xtype: 'hidden',
                    name: 'id'
                },
                {
                    xtype: 'textfield',
                    name: 'cardid',
                    allowBlank: false,
                    maxLength: 18,
                    regex: /[\d]{6}(19|20)*[\d]{2}((0[1-9])|(11|12))([012][\d]|(30|31))[\d]{3}[xX\d]*/,
                    regexText: '请输入正确的18位身份证号码！',
                    emptyText: '身份证号码不能为空',
                    fieldLabel: '身份证'
                },
                {
                    xtype: 'textfield',
                    name: 'number',
                    regex: /^[0-9]*$/,
                    regexText: '学号只能是数字！',
                    emptyText: '请输入学号...',
                    fieldLabel: '学号'
                },
                {
                    xtype: 'textfield',
                    name: 'username',
                    allowBlank: false,
                    regex: /^[\u4e00-\u9fa5]+$/,
                    regexText: '姓名只能为中文！',
                    emptyText: '姓名不能为空',
                    fieldLabel: '姓名'
                },
                {
                    xtype: 'textfield',
                    name: 'classid',
                    id: 'ssclass',
                    hidden: true
                },
                {
                    xtype: 'textfield',
                    name: 'phone',
                    regex: /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/,
                    regexText: '请输入正确的电话号码！',
                    emptyText: '请输入电话号码...',
                    fieldLabel: '电话号码'
                },
                {
                    xtype: 'textfield',
                    name: 'email',
                    vtype: 'email',
                    emptyText: '请输入电子邮箱...',
                    fieldLabel: '电子邮箱'
                }
            ],
            buttons: [{
                text: '确定',
                scope: this,
                handler: function() {
                    this.submit();
                }
            },
                {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        var f = this.form.getForm();
                        f.reset();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
                    }
                }]
        });
        return form;
    },

    getDialog: function(){
        var dlg = new Ext.Window({
            height: 280,
            width: 400,
            layout: 'fit',
            border: false,
            frame: true,
            title: '',
            closeAction: 'hide',
            modal: true,
            plain: true,
            resizable: false,
            items: [this.form],
            listeners: {
                scope: this,
                render: function(fp){
                    this.form.getForm().waitMsgTarget = fp.getEl();
                },
                show: function(){
                    var f = this.form.getForm();
                    f.setValues(this.currentFormValues);
                    f.clearInvalid();
                }
            }
        });
        return dlg;
    },

    submit: function(){
        if (this.form.getForm().isValid()) {
            var id = this.form.getForm().findField("id").getValue();
            this.form.getForm().submit({
                waitTitle: '保存数据',
                waitMsg: '正在保存……',
                scope: this,
                method: 'post',
                params: '',
                submitEmptyText: false,     //禁止提交emptyText
                success: function(form, action) {
                    var info = action.result.info;
                    this.dlg.hide();
                    Ext.Msg.alert('信息', info);
                },
                failure: function(form, action) {
                    var info = action.result.info;
                    Ext.Msg.alert('提示', info);
                }
            });
        }
    }
});

//实例化主程序类
Ext.BLANK_IMAGE_URL = 'js/ext/resources/images/default/s.gif';
Ext.form.Field.prototype.msgTarget = 'title';
Ext.onReady(function(){
    //初始化Ext.QuickTips，以使得tip提示可用
    Ext.QuickTips.init();
    myApp = new app.exam();
});